//
//  PlanNewsController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/10.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "PlanNewsController.h"
#import "PlanNewsHeadView.h"
#import "PlanNewsHeadView01.h"
#import "PlanNewsViewCell.h"
#import "PlanNewsClassModel.h"
#import "PlanNewsCellModel.h"

#import "PlanUpLoadController.h"

@interface PlanNewsController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *PlanTable;
@property (strong, nonatomic) NSString * NumbStr;
@property (strong, nonatomic) NSString * AllPrice;
@property (strong, nonatomic) NSString * Count;
@property (strong, nonatomic) NSString * Time;
@property (strong, nonatomic) NSMutableArray * ClassArr;
@property (strong, nonatomic) NSMutableArray * CellArr;



@end

@implementation PlanNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"计划详情";
    
    if (self.ShowBot) {
        [self.BottonBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [self UpDataWithDic:self.DataDic];

    }else{
        [self.BottonBtn setTitle:@"终止计划" forState:UIControlStateNormal];
        [self UploadDataOfPlanNewsController];

    }
    self.PlanTable.delegate = self;
    self.PlanTable.dataSource = self;
    self.PlanTable.rowHeight = 50;
    self.PlanTable.backgroundColor = KRGBColor(243, 243, 243);
    self.PlanTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.PlanTable registerNib:[UINib nibWithNibName:@"PlanNewsViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}

- (void)UploadDataOfPlanNewsController{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.CardModel.card_id forKey:@"card_id"];
    
    [MainObjectWay PostWithMethod:@"details" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"获取信息= %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableDictionary * dic = responseObject[@"data"];
            [self UpDataWithDic:dic];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
}

- (void)UpDataWithDic:(NSMutableDictionary *)dic{
    self.NumbStr = [NSString stringWithFormat:@"%@",dic[@"orderNo"]];
    self.AllPrice = [NSString stringWithFormat:@"%@",dic[@"orderMoney"]];
    self.Count = [NSString stringWithFormat:@"%@",dic[@"HKcount"]];
    self.Time = [NSString stringWithFormat:@"%@",dic[@"date"]];
    NSMutableArray * arr = dic[@"list"];
    for (int i = 0; i < arr.count; i ++) {
        NSMutableDictionary * dic = arr[i];
        PlanNewsClassModel * ClassModel = [PlanNewsClassModel mj_objectWithKeyValues:dic];
        [self.ClassArr addObject:ClassModel];
        
        NSMutableArray * Arr01 = dic[@"log"];
        NSMutableArray * Arr02 = [[NSMutableArray alloc] init];
        for (int i = 0; i < Arr01.count; i ++ ) {
            NSMutableDictionary * cellDic = Arr01[i];
            PlanNewsCellModel * model = [PlanNewsCellModel mj_objectWithKeyValues:cellDic];
            [Arr02 addObject:model];
        }
        [self.CellArr addObject:Arr02];
        
    }
    [self.PlanTable reloadData];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.ClassArr.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else{
        NSMutableArray * arr = self.CellArr[section-1];
        return arr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlanNewsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[PlanNewsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.ShowBot) {
        cell.IsShow = NO;
    }else{
        cell.IsShow = YES;
    }
    cell.IsShow = !self.ShowBot;
    PlanNewsCellModel * model = self.CellArr[indexPath.section-1][indexPath.row];
    [cell setModel:model];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        CGSize size = CGSizeMake(KScreenW - 100, MAXFLOAT);//设置高度宽度的最大限度
        CGRect rect = [self.Time boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20]} context:nil];
        
       
        return 100+rect.size.height;
    }else{
        return 42;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        PlanNewsHeadView * headV = [[NSBundle mainBundle] loadNibNamed:@"PlanNewsHeadView" owner:self options:nil][0];
        headV.OrderNumbLabel.text = [NSString stringWithFormat:@"%@",self.NumbStr];
        headV.AllPriceLabel.text = [NSString stringWithFormat:@"%@",self.AllPrice];
        headV.PriceCountLabel.text = [NSString stringWithFormat:@"%@",self.Count];
        headV.HKTimeLabel.text = [NSString stringWithFormat:@"%@",self.Time];
        return headV;
    }else{
        PlanNewsHeadView01 * head = [[NSBundle mainBundle] loadNibNamed:@"PlanNewsHeadView01" owner:self options:nil][0];
        PlanNewsClassModel * model = self.ClassArr[section-1];
        [head setModel:model];
        return head;
    }
}

- (IBAction)ClickOnBtnOfPlanNewsBottonBtn:(id)sender {
    if (self.ShowBot) {
        PlanUpLoadController * PlanC = [[PlanUpLoadController alloc] init];
        PlanC.CardModel = self.CardModel;
        PlanC.SNNumb = self.NumbStr;
        [self.navigationController pushViewController:PlanC animated:YES];
    }else{
        NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
        [dic setObject:KUserID forKey:@"card_member"];
        [dic setObject:self.NumbStr forKey:@"card_order"];
        
        [MainObjectWay PostWithMethod:@"cancel" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
            NSLog(@"挂靠成功 = %@",responseObject);
            if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
                [MBProgressHUD showAlertViewWithText:@"终止成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
            }
        } failure:^(NSError *error) {
            NSLog(@"错误信息 = %@",error);
        }];
    }
}

- (NSMutableArray *)ClassArr {
    if (!_ClassArr) {
        _ClassArr = [NSMutableArray array];
    }
    return _ClassArr;
}

- (NSMutableArray *)CellArr {
    if (!_CellArr) {
        _CellArr = [NSMutableArray array];
    }
    return _CellArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
